package gui.server;

import gui.ButtonName;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import data.User;

public class UserListPanel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	
	private List<User> userData=null;
	private ActionListener btnListener=null;
	
	private JTextField userId;
	private JTextField userName;
	private JTextField userPassword;
	private JRadioButton userEnable;
	private JRadioButton userDisable;
	
	private JList userList=null;
	private JScrollPane scroll=null;
	
	private JButton btnSaveUser=null;
	private JButton btnDeleteUser=null;
	private User currentUser=null;
	public UserListPanel(List<User> data,ActionListener al){
		this.userData=data;
		this.btnListener=al;
		
		initPanel();
		setEventListener();
	}
	
	private void setEventListener(){
		btnSaveUser.addActionListener(btnListener);
		btnDeleteUser.addActionListener(btnListener);
		userList.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				int index = userList.locationToIndex(e.getPoint());
		    	 currentUser=userData.get(index);
		    	 System.out.println("Double clicked on Item " + currentUser+"  state"+currentUser.getState());
		    	 showCurrentUser();
			
		}});
	}
	
	private void showCurrentUser(){
		if(currentUser!=null){
			userName.setText(currentUser.getUsername());
			userPassword.setText(currentUser.getPassword());
			userId.setText(Integer.toString(currentUser.getId()));
			if(currentUser.getState()==1){
				userEnable.setSelected(true);
				userDisable.setSelected(false);
			}else{
				userDisable.setSelected(true);
				userEnable.setSelected(false);
			}
		}
	}
	
	public User getCurrentUser() {
		if(currentUser!=null){
			int state=userEnable.isSelected()?1:2;
			currentUser.setState(state);
		}
		return currentUser;
	}

	private void initPanel(){
		JLabel id=new JLabel("User Id");
		JLabel name=new JLabel("Name");
		JLabel pwd=new JLabel("Password");
		JLabel state=new JLabel("state");
		
		userId=new JTextField();
		userId.setEditable(false);
		userName=new JTextField();
		userName.setEditable(false);
		userPassword=new JTextField();
		
		userEnable=new JRadioButton("Enable");
		userDisable=new JRadioButton("Disable");
		ButtonGroup gn=new ButtonGroup();
		gn.add(userEnable);
		gn.add(userDisable);
		JPanel btns=new JPanel(new GridLayout(1,2));
		btns.add(userEnable);
		btns.add(userDisable);
		
		this.setLayout(new GridLayout(1,2));
		JPanel right=new JPanel(new GridLayout(5,2));
		
		
		right.add(id);right.add(userId);
		right.add(name);right.add(userName);
		right.add(pwd);right.add(userPassword);
		right.add(state);right.add(btns);
		right.setBorder(BorderFactory.createTitledBorder("User Info"));
		
		btnSaveUser=new JButton(ButtonName.UPDATE_USER);
		
		
		btnDeleteUser=new JButton(ButtonName.DELETE_USER);
		
		
		
		right.add(btnSaveUser);
		right.add(btnDeleteUser);
		
		
		
		Vector<User> data=new Vector<User>();
		for (User user : userData) {
			data.add(user);
		}
		userList=new JList(data);
		scroll=new JScrollPane();
		scroll.setViewportView(userList);
		
		
		add(scroll);
		add(right);
	}
	

}
